<?php

/**
 * Front Controller plug in to set up the view with the Places view helper
 * path and some useful request variables.
 *
 */
class My_Controller_Plugin_View extends Zend_Controller_Plugin_Abstract
{

    /**
     * @var Zend_View
     */
    protected $_view;
    
    public function dispatchLoopStartup(Zend_Controller_Request_Abstract $request)
    {
        $moduleName = $request->getModuleName();
        
    	$view = new Zend_View;
        $view->setHelperPath(APPLICATION_PATH . '/modules/default/views/helpers', 'ViewHelper_')
             ->addHelperPath(APPLICATION_PATH . '/modules/' . $moduleName . '/views/helpers', 'ViewHelper_');
        
        // Set up Dojo
        Zend_Dojo::enableView($view);
        Zend_Dojo_View_Helper_Dojo::setUseDeclarative(true);
		
		$lang = Zend_Registry::get('Zend_Locale')->getLanguage();
		$stage = Zend_Registry::get('envStage'); 
        
        $view->dojo()->setDjConfigOption('preventBackButtonFix', false)
                     ->setDjConfigOption('isDebug', false)
					 ->setDjConfigOption('locale', $lang);
                     
        if($stage=='production')
        {
        	$view->dojo()->setLocalPath($view->baseUrl.'/static/common/dojo/dojo.js')
        				 ->addLayer($view->baseUrl.'/static/common/dojo/mailinh.js')
					 	 ->addLayer($view->baseUrl.'/static/common/dojo/nls/mailinh_'+$lang+'.js');
        }
		else
		{
			$view->dojo()->setLocalPath($view->baseUrl.'/static/common/dojo/dojo.js');
		}
					 	 
        $view->dojo()->disable();
                 
        Zend_Registry::set('view', $view);
        
		$viewRenderer = Zend_Controller_Action_HelperBroker::getStaticHelper('ViewRenderer');
		$viewRenderer->setView($view);
		
        // Modular Layout
		$layout = Zend_Layout::startMvc(array(
		    'layoutPath' => APPLICATION_PATH . '/modules/' . $moduleName . '/layouts'
		));
        
        $layout->setLayout('default');
    }
}